package com.vincce.sport.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.vincce.sport.core.utils.CheckUtil;
import com.vincce.sport.entity.Page;
import com.vincce.sport.entity.Zone;
import com.vincce.sport.mapper.ZoneMapper;

@Controller
@RequestMapping("/zone")
public class ZoneController extends BaseController{
	@Resource
	private ZoneMapper zoneMapper;
	@RequestMapping("/zoneList")
	public String queryZoneByPage(Model model,Page<Zone> page,Zone zone){
		Map<String, Object> filter = new HashMap<String, Object>();
		if(zone.getName() != null && !zone.getName().equals("")){
	            filter.put("name", zone.getName());
	    }
		page.setFilter(filter);
		page.setTotal(zoneMapper.queryByZoneCount(page.getFilter()));
		page.setResult(zoneMapper.queryByZonePage(page));
		model.addAttribute("page",page);
		return "/backstage/zoneList";
	}
	@RequestMapping("/addZone")
	@ResponseBody
	public String insertZone(Model model,Zone zone){
		Boolean flag=false;
		zone.setCreateUser(getCurrentUser().getId().intValue());
		zone.setCreateTime(new Date());
		Long count=zoneMapper.insertZone(zone);
		flag=count>0?true:false;
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("suc", flag);
        return JSON.toJSONString(result);
	}
	@RequestMapping("/updateZone")
	@ResponseBody
	public String updateZone(Model model,Zone zone){
		Boolean flag=false;
		
		Long count=zoneMapper.updateZone(zone);
		flag=count>0?true:false;
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("suc", flag);
        return JSON.toJSONString(result);
	}
	@RequestMapping("/deleteById")
	@ResponseBody
	public String deleteById(Model model,Integer id){
		Boolean flag=false;
		Integer count=zoneMapper.deleteById(id);
		flag=count>0?true:false;
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("suc", flag);
        return JSON.toJSONString(result);
	}
	
	
	@RequestMapping("/checkUnique")
	@ResponseBody
	public String checkUnique(Zone zone) throws IOException{
		Map<String, Object> map = new HashMap<String, Object>();
		if(!CheckUtil.isEmpty(zone.getName())){
			map.put("name", zone.getName());
		}
		
		if(!CheckUtil.isEmpty(zone.getId())){
			map.put("id", zone.getId());
		}
		
		Zone brandListJson = zoneMapper.checkUniqueZone(map);
		boolean flag = false;
		if(brandListJson == null || brandListJson.equals("") || brandListJson.equals("{}")){
			flag = true;
		}
		return JSON.toJSONString(flag);
	}   
    

}
